//
//  HMComposeTextView.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/16.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit
import SnapKit
class HMComposeTextView: UITextView,UITextViewDelegate {

    
    
    @IBInspectable var placeholder: String? {
        didSet {
            placeLable.text = placeholder
        }
    }
    override var text: String? {
    
        didSet{
            placeLable.hidden = hasText()
        }
    }
    
    override var font: UIFont? {
        didSet{
            //   指定占位文字控件的字体大小
            placeLable.font = font
        }
    }
    
  lazy  var placeLable:UILabel = {
    
        let lable = UILabel()
    
        lable.text = "我是占位字符"
    
        lable.font = UIFont.systemFontOfSize(12)
    
        lable.tintColor = UIColor.grayColor()

        return lable
    }()
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        steupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func  steupUI(){
    
        addSubview(placeLable)
        
      placeLable.translatesAutoresizingMaskIntoConstraints = false
        translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.LessThanOrEqual, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant:  -10))
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChanged", name:UITextViewTextDidChangeNotification , object: nil)
        delegate = self
    }
    
  @objc  func textChanged(){
       placeLable.hidden = hasText()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        placeLable.x = 5
        placeLable.y = 8
    }

}
